package service.impl;

import dao.ClockDao;
import dao.ClockRuleDao;
import dao.impl.ClockDAOImpl;
import dao.impl.ClockRuleDaoImpl;
import entity.ClockInRecord;
import entity.ClockRule;
import service.ClockRuleService;
import service.ClockService;

import javax.naming.NamingException;
import java.sql.SQLException;
import java.time.LocalTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class ClockServicelmpl implements ClockService {
    ClockDao clockDao=new ClockDAOImpl();
  ClockRuleService clockRuleService=new ClockRuleServiceImpl();
    @Override
 public boolean saveCheckIn(ClockInRecord record) throws SQLException, ClassNotFoundException, NamingException {

        // 先检查是否已打卡
    if(hasCheckedInToday(record.getStudentId())) {
        return false;
    }
    // 验证打卡时间是否在有效时间段内
    boolean isValid = clockRuleService.isValidCheckInTime(record.getClockTime());
    record.setStatus(isValid ? 1 : 0); // 设置状态
    
    // 无论是否有效都保存记录
    return clockDao.saveCheckIn(record);
}

    @Override
    public boolean hasCheckedInToday(String studentId) throws SQLException, NamingException, ClassNotFoundException {
        return clockDao.hasCheckedInToday(studentId);
    }

     @Override
    public List<ClockInRecord> getClockRecords(Date startDate, Date endDate) throws SQLException, ClassNotFoundException {
        return clockDao.getClockRecords(startDate, endDate);
    }
}
